const URL_REPLACEMENT_MAP = {
    'https://everia.club/': '/everia/',
    'https://quenbox.com/': '/quenbox/'
};

// 缓存已处理的URL
const URL_CACHE = new Map();

export function replaceUrl(url) {
    const urlStr = String(url);
    if (!urlStr) return urlStr;
    
    for (const [prefix, replacement] of Object.entries(URL_REPLACEMENT_MAP)) {
        if (urlStr.startsWith(prefix)) {
            const result = replacement + urlStr.slice(prefix.length);
            if (process.env.NODE_ENV === 'development') {
                console.debug(`URL replaced: ${urlStr} -> ${result}`);
            }
            return result;
        }
    }
    
    return urlStr;
}

export function getLastPathSegment(url) {
    // 移除可能的查询参数和哈希
    const path = url.split('?')[0].split('#')[0];
    // 分割路径并过滤空项（例如结尾的/）
    const segments = path.split('/').filter(segment => segment !== '');
    // 返回最后一项
    return segments.pop();
}